﻿#ifndef __SS_NET_BASE_H__
#define __SS_NET_BASE_H__

#ifdef SS_USE_WINDOWS
#include <WinSock2.h>
#include <ws2ipdef.h>
#include <WS2tcpip.h>
#define NONBLOCK_ERROR_1 10035
#define NONBLOCK_ERROR_2 10036
#define SIO_KEEPALIVE_VALS _WSAIOW(IOC_VENDOR,4)
struct ssTcpKeepalive
{
	unsigned long  onoff;
	unsigned long  keepalivetime;
	unsigned long  keepaliveinterval;
	unsigned long  keepCount;
};
#else
#include <errno.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <string.h>
#define INVALID_SOCKET (-1)
#define SOCKET_ERROR (-1)
typedef int SOCKET;
typedef sockaddr SOCKADDR;
#define NONBLOCK_ERROR_1 11
#define NONBLOCK_ERROR_2 4
#endif
#include <string>
#define NONBLOCK_ERROR_3 EWOULDBLOCK

namespace StarSeeker
{

/*enum eFlags
{
	FlagsNone = 0,
	OutOfBand = MSG_OOB,		// 处理带外数据。
	Peek = MSG_PEEK,			// 查看传入的消息
	DontRoute = MSG_DONTROUTE,	// 不使用路由表发送
	WaitAll = MSG_WAITALL,		// 阻塞时间 不能和非阻塞一起用
	Partial = MSG_PARTIAL,		// 消息的部分发送或接收
};*/

//加载Socket库
bool LoadSocketLib();
//卸载Socket库
void UnloadSocketLib();
//获取错误代码
int GetSocketError();
//关闭socket
void CloseOneSocket(SOCKET &s);
//监听
int ListenPort(const char* ip, const unsigned short port, SOCKET &s, sockaddr_in* paddr=0);
int ListenPortV6(const char* ip, const unsigned short port, SOCKET &s, sockaddr_in6* paddr = 0);
//连接
int ConnectToServer(const char *ip, const unsigned short port, SOCKET &s, sockaddr_in* paddr = 0);
int ConnectToServerV6(const char *ip, const unsigned short port, SOCKET &s, sockaddr_in6* paddr = 0);
//分配连接
SOCKET AcceptListen(SOCKET listenSocket, sockaddr_in* paddr = 0);
SOCKET AcceptListenV6(SOCKET listenSocket, sockaddr_in6* paddr = 0);
// 获得本机的IP地址
void GetLocalIP(char *ip);
//设置非阻塞socket
void Nonblock(SOCKET& s);
//设置缓冲区
bool SetSendBuffer(SOCKET &s, unsigned int size);
bool SetRecvBuffer(SOCKET &s, unsigned int size);
//设置心跳(间隔,多久后开始检测,检测的最大次数),单位毫秒
bool SetKeepAlive(SOCKET &s, bool alive, int keepaliveinterval, int keepalivetime, int keepCount);
//域名解析
std::string DomainNameRes(const char* domainStr);
//是否ipv6网络
bool IsIPv6Net(const char * domainStr="www.baidu.com");

}
#endif
